package com.example.lending.View;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.UserViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class regFragment extends Fragment {
    Toolbar toolbar;
    ImageView imageView1,imageView2;
    EditText Username,Password,Password2,Emial;
    Button reg,back;
    UserViewModel userViewModel;
    LiveData<List<User>> liveuser;
    public regFragment() {
        // Required empty public constructor
    }
    public void inteView(){
        imageView1=getView().findViewById(R.id.imageView3);
        imageView2=getView().findViewById(R.id.imageView4);
        Username=getView().findViewById(R.id.et_username);
        Password=getView().findViewById(R.id.et_password);
        Password2=getView().findViewById(R.id.et_password2);
        Emial=getView().findViewById(R.id.et_email);
        reg=getView().findViewById(R.id.btn_reg);
        back=getView().findViewById(R.id.btn_back);
        userViewModel=ViewModelProviders.of(getActivity(),new SavedStateViewModelFactory(getActivity().getApplication(),getActivity())).get(UserViewModel.class);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inteView();
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Password.getText().toString().equals(Password2.getText().toString())){
                    if (TextUtils.isEmpty(Password.getText().toString())||TextUtils.isEmpty(Username.getText().toString())||TextUtils.isEmpty(Emial.getText().toString())||
                            TextUtils.isEmpty(Password2.getText().toString())){
                        Toast.makeText(getActivity(), "请补充填完所有数据", Toast.LENGTH_SHORT).show();
                    }else {
                        //liveuser = userViewModel.getAllUserLive();
                        liveuser = userViewModel.findUserWithPattern(Username.getText().toString());
                        if (liveuser.getValue()!=null) {
                            Toast.makeText(getActivity(), "该用户名已经被使用。。。", Toast.LENGTH_SHORT).show();
                        } else {
                            User user = getUser(Username.getText().toString(), Password.getText().toString(), Emial.getText().toString());
                            userViewModel.insertUsers(user);
                            Toast.makeText(getActivity(), "注册成功", Toast.LENGTH_SHORT).show();
                            NavController controller = Navigation.findNavController(getActivity(),R.id.fragment4);
                            controller.navigate(R.id.action_regFragment_to_loginFragment2);
                        }
                    }
                }else{
                    System.out.println("3");
                    Toast.makeText(getActivity(), "两次密码不相等", Toast.LENGTH_SHORT).show();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller=Navigation.findNavController(v);
                controller.navigate(R.id.action_regFragment_to_loginFragment2);
            }
        });
    }
    public User getUser(String name ,String password,String emial){
        User user=new User();
        user.setName(name);
        user.setType(0);
        user.setPassword(password);
        user.setEmail(emial);
        return user;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_reg, container, false);
    }

}
